package GUILayer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.BoxLayout;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import ControlLayer.SupplierCtr;
import ModelLayer.Supplier;
import javax.swing.BorderFactory;
import javax.swing.JLabel;

public class ProductSupplierGUI extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTable supplierTable;
	private JTextField searchfield;
	private SupplierCtr control;
	private DefaultTableModel supplierTableModel;
	private JScrollPane scrollPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;

	/**
	 * Create the dialog.
	 */
	public ProductSupplierGUI() {
		super();
		control = new SupplierCtr();
		init();
	}
	
	public ProductSupplierGUI(int supplierID) {
		super();
		control = new SupplierCtr();
		Supplier supplier = control.getSupplier(supplierID);
		init();
	}
	
	public void show() {
		init();
	}
	
	private void init() {
		
		setBounds(100, 100, 600, 480);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel listPanel = new JPanel();
			contentPanel.add(listPanel, BorderLayout.WEST);
			listPanel.setLayout(new BorderLayout(0, 0));
			{
				JPanel searchPanel = new JPanel();
				listPanel.add(searchPanel, BorderLayout.NORTH);
				{
					searchfield = new JTextField();
					searchfield.setColumns(10);
				}
				GroupLayout gl_searchPanel = new GroupLayout(searchPanel);
				gl_searchPanel.setHorizontalGroup(
					gl_searchPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_searchPanel.createSequentialGroup()
							.addGap(183)
							.addComponent(searchfield)
							.addGap(183))
				);
				gl_searchPanel.setVerticalGroup(
					gl_searchPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_searchPanel.createSequentialGroup()
							.addGap(5)
							.addComponent(searchfield, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				);
				searchPanel.setLayout(gl_searchPanel);
			}
			{
				JScrollPane scrollPane = new JScrollPane();
				listPanel.add(scrollPane, BorderLayout.CENTER);
			}
		}
		{
			JPanel panel = new JPanel();
			panel.setBorder(BorderFactory.createTitledBorder("Info"));
			contentPanel.add(panel, BorderLayout.CENTER);
			JLabel label = new JLabel("Supplier Id: ");
			JLabel label_1 = new JLabel("Name: ");
			JLabel label_2 = new JLabel("Address: ");
			JLabel label_3 = new JLabel("Country: ");
			JLabel label_4 = new JLabel("Phone: ");
			JLabel label_5 = new JLabel("E-mail: ");
			textField = new JTextField();
			textField.setEditable(false);
			textField_1 = new JTextField();
			textField_2 = new JTextField();
			textField_3 = new JTextField();
			textField_4 = new JTextField();
			textField_5 = new JTextField();
			GroupLayout gl_panel = new GroupLayout(panel);
			gl_panel.setHorizontalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
					.addGap(0, 359, Short.MAX_VALUE)
					.addGroup(gl_panel.createSequentialGroup()
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
							.addComponent(label)
							.addComponent(label_1)
							.addComponent(label_2)
							.addComponent(label_3)
							.addComponent(label_4)
							.addComponent(label_5))
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
							.addComponent(textField, 265, 265, 265)
							.addComponent(textField_1, 265, 265, 265)
							.addComponent(textField_2, 265, 265, 265)
							.addComponent(textField_3, 265, 265, 265)
							.addComponent(textField_4, 265, 265, 265)
							.addComponent(textField_5, 265, 265, 265)))
			);
			gl_panel.setVerticalGroup(
				gl_panel.createParallelGroup(Alignment.LEADING)
					.addGap(0, 195, Short.MAX_VALUE)
					.addGroup(gl_panel.createSequentialGroup()
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label)
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_1)
							.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_2)
							.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_3)
							.addComponent(textField_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_4)
							.addComponent(textField_4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
							.addComponent(label_5)
							.addComponent(textField_5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
			);
			gl_panel.setAutoCreateGaps(true);
			gl_panel.setAutoCreateContainerGaps(true);
			panel.setLayout(gl_panel);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	private void createTable() {
		
	}
	
	private void populateTable() {
		supplierTableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		supplierTableModel.addColumn("ID");
		supplierTableModel.addColumn("Supplier");
		supplierTableModel.addColumn("Country");


		supplierTable = new JTable(supplierTableModel);
		supplierTable.setFillsViewportHeight(true);
		supplierTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		supplierTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
		supplierTable.setAutoCreateRowSorter(true);

		supplierTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {

			}
		});

		scrollPane.setViewportView(supplierTable);
	}
}
